/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
          /*LINTLIBRARY*/
          /*PROTOLIB1*/
/*#include <math.h>*/
#include "main.h"
#include "acb_code.h"



/* Note: The following table was converted to 1.14 fixed-point format */
/* (multiply by 2^14) */
static fxpt_16 ACBGainTable[32] =
{
-16269, -13615, -11354,  -9093,  -6783,  -3752,      0,   2277,
  4178,   6029,   7487,   8700,   9847,  10699,  11502,  12206,
 12780,  13369,  13926,  14434,  14991,  15532,  16105,  16712,
 17400,  18301,  19546,  21119,  22839,  25231,  28918,  32621
};

/**************************************************************************
*
* ROUTINE
*               ACBGainEncode
*
* FUNCTION
*
*               encode and quantize ACB gain for various
*               quantizer types.
*
* SYNOPSIS
*               ACBGainEncode(UGain, *QGain)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       UGain           fxpt_16 i       pitch gain input (true value)
*       QGain           int     o       encoded pitch gain index
*
*       ACBGainEncode   fxpt_16 fun     encoded pitch gain
*
***************************************************************************
*
* DESCRIPTION
*
*       This funtion uses output level data obtained by Max's minimum
*       distortion quantization priciples and quantizes to the nearest
*       level (L1 norm).  (Using level data only was found superior to
*       using both of Max's level and boundry data.)
*
***************************************************************************
*
* CALLED BY
*
*       AdaptiveAnalysis Encode Decode
*
***************************************************************************
*
* REFERENCES
*
*       Quantizing for Minimum Distorion
*       J. Max
*       IRE Trans. Inform. Theory, vol. IT-6, pp.7-12, Mar. 1960
*
**************************************************************************/
fxpt_16 ACBGainEncode(fxpt_16 UGain, int *QGain)
{
        int i;
        fxpt_32 gain;
        fxpt_32 dist, low;

FXPT_PUSHSTATE("ACBGainEncode", -1.0, -1.0);
        gain = fxpt_deposit_l(UGain);
        low = dist = fxpt_abs32(fxpt_sub32(gain,
            fxpt_deposit_l(ACBGainTable[0])));
        *QGain = 0;
        for (i=1; i<32; i++) {
                dist = fxpt_abs32(fxpt_sub32(gain,
                    fxpt_deposit_l(ACBGainTable[i])));
                if (dist < low) {
                        low = dist;
                        *QGain = i;
                }
        }

FXPT_POPSTATE();
        return (ACBGainTable[*QGain]);
}

/**************************************************************************
*
* ROUTINE
*               ACBGainDecode
*
* FUNCTION
*
*               decode ACB gain from table
*
* SYNOPSIS
*               ACBGainDecode(QGain, UGain)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       QGain           int     i       Quantized ACB Gain
*       UGain           fxpt_16 o       Unquantized ACB Gain
*
***************************************************************************
*
*       The data used in the table generation is from 3m3f.spd.
*
***************************************************************************
*
* REFERENCES
*
*       Quantizing for Minimum Distorion
*       J. Max
*       IRE Trans. Inform. Theory, vol. IT-6, pp.7-12, Mar. 1960
*
**************************************************************************/
void ACBGainDecode(int QGain, fxpt_16 *UGain)
{
        *UGain = ACBGainTable[QGain];
}
